home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Internet Chooser / reggie / basic / stddebug.h < prev    next >
Text File  |  1996-06-22  |  6KB  |  148 lines

  1. /* File "stddebug.h", Light Sockets - Copyright (C) Matt Slot, 1996           */
  2. /* Standard debugging and error tracking macros.                              */
  3.  
  4. #ifndef __STD_DEBUG_HEADER__
  5. #define __STD_DEBUG_HEADER__
  6.  
  7. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  8. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  9. /* Functional Summary */
  10.  
  11. /*
  12. Wrap function calls, default conditionals, and sanity-checks with the
  13. following wrappers to provide error testing and reporting on MacOS and
  14. ANSI platforms. Functions are divided into 4 categories:
  15.  
  16.     ASSERT - Debugging-only utility used to sanity-checking pointers and
  17.              parameters that *should* always be valid, but for which you
  18.              have provided an external API to another developer. Debug
  19.              software will log a message and exit the program; non-debug
  20.              software should completely optimize the tests out.
  21.     
  22.     TRACE -  Simple logging utility for recording unusual circumstances
  23.              or soft errors which must be ignored. Tracing evaluates and
  24.              sets the "error" code, but does not stop or branch execution.
  25.  
  26.     THROW -  Error testing and logging facility for hard errors, which 
  27.              force execution to branch. Require the presence of a CATCH
  28.              function toward the end.
  29.  
  30.     CATCH -  Zero-overhead function which acts as the target of execution
  31.              after a THROW event. Commands after a CATCH should dispose 
  32.              structures while carefully testing for exceptional or error
  33.              cases.
  34.  
  35. By redefining _DEBUGFTN, it is possible to change the method by which errors
  36. are reported. Be careful, however, because only certain report functions are 
  37. interrupt-safe. Finally, the _DEBUG #define differentiates between compiling
  38. a debug version (much larger, slower) for internal or beta testing, and a
  39. non-debug version for sending to end-users.
  40. */
  41.  
  42. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  43. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  44. /* Include Files */
  45.  
  46.  
  47. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  48. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  49. /* Preprocessor Defines */
  50.  
  51. #if defined(__MWERKS__)
  52. #define PLATFORM_MAC
  53. #endif
  54.  
  55. #define _DEBUG
  56.  
  57. #if defined(PLATFORM_MAC)
  58. #define _DEBUGFTN   _DebugMacsbug
  59. #else
  60. #define _DEBUGFTN   _DebugPrintErr
  61. #endif
  62. #define _DEBUGSAFE  ((_DEBUGFTN == _DebugMacsbug)|| (_DEBUGFTN == _DebugNotify))
  63.  
  64. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  65. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  66.  
  67. #ifndef _DEBUG
  68.  
  69.     #define qAssert()         (0)
  70.     #define qAssertMsg(m)     (0)
  71.     #define qAssertErr(e,m)   (0)
  72.  
  73.     #define qTrace()          (0)
  74.     #define qTraceMsg(m)      (0)
  75.     #define qTraceErr(e,m)    (error = (e))
  76.  
  77.     #define qThrow()          { goto _CLEANUP; }
  78.     #define qThrowMsg(m)      { goto _CLEANUP; }
  79.     #define qThrowErr(e,m)    { (error = (e)); goto _CLEANUP; }
  80.     
  81. #else
  82.  
  83.     /* Kludges to make the preprocessor convert __LINE__ into a string */
  84.     #define _mkstr(x)         _mkval(x)
  85.     #define _mkval(x)         #x
  86.  
  87.     #define _FL                    __FILE__
  88.     #define _LN                    _mkstr(__LINE__)
  89.  
  90.     #define qAssert()         { _DEBUGFTN(0,0,_FL,_LN,0,1); }
  91.     #define qAssertMsg(m)     { _DEBUGFTN((m),0,_FL,_LN,0,1); }
  92.     #define qAssertErr(e,m)   { _DEBUGFTN((m),error=(e),_FL,_LN,0,1); }
  93.  
  94.     #define qTrace()          { _DEBUGFTN(0,0,_FL,_LN,0,0); }
  95.     #define qTraceMsg(m)      { _DEBUGFTN((m),0,_FL,_LN,0,0); }
  96.     #define qTraceErr(e,m)    { _DEBUGFTN((m),error=(e),_FL,_LN,0,0); }
  97.  
  98.     #define qThrow()          { _DEBUGFTN(0,0,_FL,_LN,1,0); goto _CLEANUP; }
  99.     #define qThrowMsg(m)      { _DEBUGFTN((m),0,_FL,_LN,1,0); goto _CLEANUP; }
  100.     #define qThrowErr(e,m)    { _DEBUGFTN((m),error=(e),_FL,_LN,1,0); \
  101.                                   goto _CLEANUP; }
  102.  
  103. #endif /* _DEBUG */
  104.  
  105. #define qAssertIfError(e,m)   { long _e; if (_e=(e)) qAssertErr(_e,(m));}
  106. #define qAssertIfNull(x,e,m)  { if (!(x)) qAssertErr((e),(m)); }
  107. #define qAssertIfFalse(x,e,m) { if (!(x)) qAssertErr((e),(m)); }
  108. #define qAssertIfTrue(x,e,m)  { if (x) qAssertErr((e),(m)); }
  109.  
  110. #define qTraceIfError(e,m)    { long _e; if (_e=(e)) qTraceErr(_e,(m)); }
  111. #define qTraceIfNull(x,e,m)   { if (!(x)) qTraceErr((e),(m)); }
  112. #define qTraceIfFalse(x,e,m)  { if (!(x)) qTraceErr((e),(m)); }
  113. #define qTraceIfTrue(x,e,m)   { if (x) qTraceErr((e),(m)); }
  114.  
  115. #define qThrowIfError(e,m)    { long _e; if (_e=(e)) qThrowErr(_e,(m)); }
  116. #define qThrowIfNull(x,e,m)   { if (!(x)) qThrowErr((e),(m)); }
  117. #define qThrowIfFalse(x,e,m)  { if (!(x)) qThrowErr((e),(m)); }
  118. #define qThrowIfTrue(x,e,m)   { if (x) qThrowErr((e),(m)); }
  119.  
  120. #define qCatch()              { _CLEANUP: ; }
  121.  
  122. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  123. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  124. /* Structure/Class Declarations */
  125.  
  126.  
  127. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  128. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  129. /* Function Prototypes */
  130.  
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif
  134.  
  135. void _DebugPrintErr(char *, long, char *, char *, int, int);
  136. void _DebugPrintLog(char *, long, char *, char *, int, int);
  137.  
  138. #if defined(PLATFORM_MAC)
  139. void _DebugMacsbug(char *, long, char *, char *, int, int);
  140. void _DebugNotify(char *, long, char *, char *, int, int);
  141. #endif
  142.  
  143. #ifdef __cplusplus
  144.     }
  145. #endif
  146.  
  147. #endif /* __STD_DEBUG_HEADER__ */
  148.